package com.tt.controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding.Use;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;
import com.tt.entity.Page;
import com.tt.entity.Strategy;
import com.tt.entity.User;
import com.tt.service.AdminserviceImpl;

import javafx.css.PseudoClass;
@Controller
public class AdminController {
	
	@Resource
	private AdminserviceImpl aImpl;
	
	//登录
	@RequestMapping("Loginadmin")
	public String login(HttpServletRequest request){
		HttpSession session = request.getSession();
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		boolean a =this.aImpl.getloginadmin(username, password);
		if (a==false) {
			request.setAttribute("message1", "账号或者密码不正常！");
			return "/adminlogin.jsp";
		}					
		return "/admin.jsp";
	}
	
	//查看所有已发布拾光动态
	@RequestMapping(value="Strategy")
	public String findAllStrategy1(HttpServletRequest request,Model model){
		int pageNum = Integer.parseInt(request.getParameter("pageNum")==null||request.getParameter("pageNum").equals("")?"1":request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize")==null||request.getParameter("pageSize").equals("")?"2":request.getParameter("pageSize"));
		Page<Strategy> findAllStrategy1 = aImpl.findAll(pageNum, pageSize);
		model.addAttribute("findAllStrategy1",findAllStrategy1);
		return "/admin.jsp";
	}
	//删除已发布拾光动态
	@RequestMapping(value="deleteStrategy")
	public Object deleteStrategy(HttpServletResponse response,@RequestParam("id") Integer id) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		boolean deleteStrategy=aImpl.deleteStrategyt(id);
		PrintWriter s=response.getWriter();
		if (deleteStrategy) {
			s.print("<script language='javascript'>alert('删除成功！');window.location.href='User'</script>");
		}
		s.print("<script language='javascript'>alert('删除失败');window.location.href='User'</script>");
		return s;
	}	
	//查看所有用户信息
	@RequestMapping(value="User")
	public String User(HttpServletRequest request,Model model){
		int pageNum = Integer.parseInt(request.getParameter("pageNum")==null||request.getParameter("pageNum").equals("")?"1":request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize")==null||request.getParameter("pageSize").equals("")?"3":request.getParameter("pageSize"));
		Page<User>User=aImpl.findAll2(pageNum, pageSize);
		model.addAttribute("User",User);
		return "/admin.jsp";
	}
	//删除用户
	@RequestMapping(value="deleteUser")
	public Object deleteUser(HttpServletResponse response,@RequestParam("id") Integer id) throws IOException{
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		boolean deleteUser = aImpl.deleteUser(id);
		PrintWriter src=response.getWriter();
		if (deleteUser) {
			src.print("<script language='javascript'>alert('删除成功！');window.location.href='User'</script>");
		}
		src.print("<script language='javascript'>alert('删除失败');window.location.href='User'</script>");
		return src;
	}
	
	
	
	//显示升级为商家的用户信息
	@RequestMapping(value="upUser")
	public String upUser(HttpServletRequest request,Model model){
		Integer uid = 1;
		int pageNum = Integer.parseInt(request.getParameter("pageNum")==null||request.getParameter("pageNum").equals("")?"1":request.getParameter("pageNum"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize")==null||request.getParameter("pageSize").equals("")?"3":request.getParameter("pageSize"));
		Page<User>upUser=aImpl.findAll3(pageNum, pageSize, uid);
		model.addAttribute("upUser", upUser);
		return "/admin.jsp";
	}
	//用户升级为商家
	@RequestMapping(value="sUser/{id}")
	public String saveid(Model model,@PathVariable Integer id){
		User user = aImpl.sUser(id);
		model.addAttribute("User", user);//把该对象存储到request作用域中
		return "/admininfo.jsp";
	}
	
	@RequestMapping(value="updateUser",method=RequestMethod.POST)
	public String updateUser(User p){	
		aImpl.updateUser(p);
		return "upUser";	
	}
	
}
